//接口前缀
const baseURL='https://api-hmugo-web.itheima.net/api/public/v1';

// 封装小程序request请求
const request = (config) => {
    uni.showLoading({
      title: '加载中',
      mask:true
    })

    return new Promise((resolve,reject) => {
        uni.request({
           ...config,
           url: baseURL + config.url,
           success:(res) => {
               if(res.statusCode == 200 && res.data.meta.status === 200){
                   resolve(res.data.message);
               }
           },
           fail:(error) => {
               reject(error);
           },
           complete:() => {
               //完成后关闭加载    
               uni.hideLoading();
           }
        })
    });
}


// 多个异步请求处理
const requestAll = async(arr) => {
    uni.showLoading({
      title: '加载中',
      mask:true
    })

    await Promise.all(arr);
    uni.hideLoading();

    return Promise.resolve();
}


// 曝露
export default{
    request,
    requestAll
}